<?php

namespace Trois\MainServerRegister\Registers;

use EasySwoole\Template\RenderInterface;

class TpTemplateDriver implements RenderInterface
{
    protected $template;

    function __construct()
    {
        $config = [
            'view_path' => EASYSWOOLE_ROOT . '/App/Views/',
            'cache_path' => EASYSWOOLE_ROOT . '/Temp/runtime/',
        ];
        $this->template = new \think\Template($config);
    }

    public function render(string $template, ?array $data = null, ?array $options = null): ?string
    {
        ob_start();
        $this->template->assign($data);
        $this->template->fetch($template);
        return ob_get_contents();
    }

    public function onException(\Throwable $throwable, $arg): string
    {
        $msg = "{$throwable->getMessage()} at file:{$throwable->getFile()} line:{$throwable->getLine()}";
        trigger_error($msg);
        return $msg;
    }

}